package allTests;

import abbot.finder.matchers.ClassMatcher;
import abbot.finder.matchers.NameMatcher;
import abbot.util.AWTFixtureHelper;
import com.supermap.data.Dataset;
import com.supermap.data.Datasource;
import com.supermap.data.GeoCoordSysType;
import com.supermap.data.PrjCoordSysType;
import com.supermap.desktop.controls.ControlsProperties;
import com.supermap.desktop.core.properties.CoreProperties;
import com.supermap.desktop.process.ProcessProperties;
import com.supermap.desktop.process.tasks.TasksManager;
import operation.*;
import org.junit.*;
import org.junit.rules.ErrorCollector;
import robotCross.CrossRobot;
import robotCross.TextMatcher;
import socketEnv.DataUtil;
import util.Info;

import javax.swing.*;
import java.awt.*;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import static operation.Operation.robot;
import static operation.RibbonOperation.RIBBONOPERATION;

/**
 * Created by caolp on 2017-11-13.
 */
public class TestDataProcess {
	String picturePath = DataUtil.resultPicturePath + "TestDataProcess\\";
	@Rule
	public final ErrorCollector errorCollector = new ErrorCollector();
	CompareOperation compareOperation = CompareOperation.getInstance(errorCollector);

	@Before
	public void runBeforeTestMethod() {
		Info.out("@Before - runBeforeTestMethod");
		Operation.helper = new AWTFixtureHelper();
		Operation.robot = Operation.helper.getRobot();
		DialogOperation.closeAllDialog();
		Operation.datasources.clear();
	}

	@After
	public void runAfterTestMethod() throws Exception {
		Info.out("@After - runAfterTestMethod");
		Operation.releaseDatasources();
		DialogOperation.closeAllDialog();
		WorkspaceTreeOperation.closeWorkspace();
	}

	@Test
	public void testRegionToCenterLine() throws Exception {
		String udbPath = DataUtil.udbPath("DataProcess/DataProcess_Vector.udb");
		String dataSourceType = "打开文件型数据源...";
		new WorkspaceTreeOperation().selectTheDatasouceNodeMenu(dataSourceType);
		new JFileChooserOperation().chooseFile(udbPath);
		robot.delay(2000);
		RIBBONOPERATION.clickButton("可视化建模", "新建");
		ModelTreeOperation modelTreeOperation = new ModelTreeOperation();
		modelTreeOperation.selectTreeNode("数据处理", "矢量", "面提取中心线");
		ModelWindowOperation modelWindowOperation = new ModelWindowOperation();
		modelWindowOperation.dragToCanvasPoint(new Point(0, 50));
		//选中节点，设置参数
		modelWindowOperation.clickProcessGraph("面提取中心线");
		Operation operation = new Operation(modelWindowOperation.getContainer());
		operation.clickRightButtonMenuItem("参数设置");
		DockbarOperation dockbarOperation = new DockbarOperation("参数设置");
		dockbarOperation.setComboBoxSelectedItem(new NameMatcher(CoreProperties.getString(CoreProperties.Label_Dataset)), "RegionToCenterLine");
		dockbarOperation.editTextField(new NameMatcher(CoreProperties.getString("String_MaxWidth")), "5000");
		dockbarOperation.editTextField(new NameMatcher(CoreProperties.getString(CoreProperties.Label_Dataset) + "_textField"), "actual_RegionToCenterLine");
		//点击执行
		ModelOperation.runWorkFlow();
		//等待进度条 3min
		int i = modelWindowOperation.waitWorkFlowCompleted(TimeUnit.MINUTES.toMillis(3));
		Assert.assertEquals("任务执行失败！", TasksManager.WORKFLOW_STATE_COMPLETED, i);
		Boolean boolen = compareOperation.compareDataset("DataProcess_Vector", "exp_RegionToCenterLine", "actual_RegionToCenterLine");
		Assert.assertTrue("实际生成的结果与预计结果不一致！", boolen);
		new CommonOperation().deleteDatasets("DataProcess_Vector", "actual_RegionToCenterLine");
	}

	@Test
	public void testDoubleLineToCenterLine() throws Exception {
		String udbPath = DataUtil.udbPath("DataProcess/DataProcess_Vector.udb");
		String dataSourceType = "打开文件型数据源...";
		new WorkspaceTreeOperation().selectTheDatasouceNodeMenu(dataSourceType);
		new JFileChooserOperation().chooseFile(udbPath);
		robot.delay(2000);
		RIBBONOPERATION.clickButton("可视化建模", "新建");
		ModelTreeOperation modelTreeOperation = new ModelTreeOperation();
		modelTreeOperation.selectTreeNode("数据处理", "矢量", "双线提取中心线");
		ModelWindowOperation modelWindowOperation = new ModelWindowOperation();
		modelWindowOperation.dragToCanvasPoint(new Point(0, 50));
		//选中节点，设置参数
		modelWindowOperation.clickProcessGraph("双线提取中心线");
		Operation operation = new Operation(modelWindowOperation.getContainer());
		operation.clickRightButtonMenuItem("参数设置");
		DockbarOperation dockbarOperation = new DockbarOperation("参数设置");
		dockbarOperation.setComboBoxSelectedItem(new NameMatcher(CoreProperties.getString(CoreProperties.Label_Dataset)), "DoubleLineToCenterLine");
		dockbarOperation.editTextField(new NameMatcher(CoreProperties.getString("String_MaxWidth")), "5000");
		dockbarOperation.editTextField(new NameMatcher(CoreProperties.getString(CoreProperties.Label_Dataset) + "_textField"), "actual_DoubleLineToCenterLine");
		//点击执行
		ModelOperation.runWorkFlow();
		//等待进度条 3min
		int i = modelWindowOperation.waitWorkFlowCompleted(TimeUnit.MINUTES.toMillis(3));
		Assert.assertEquals("任务执行失败！", TasksManager.WORKFLOW_STATE_COMPLETED, i);
		Boolean boolen = compareOperation.compareDataset("DataProcess_Vector", "exp_DoubleLineToCenterLine", "actual_DoubleLineToCenterLine");
		Assert.assertTrue("实际生成的结果与预计结果不一致！", boolen);
		new CommonOperation().deleteDatasets("DataProcess_Vector", "actual_DoubleLineToCenterLine");
	}

	@Test
	public void testAggregatePoints() throws Exception {
		String udbPath = DataUtil.udbPath("DataProcess/DataProcess_Vector.udb");
		String dataSourceType = "打开文件型数据源...";
		RIBBONOPERATION.clickButton("可视化建模", "新建");
		ModelTreeOperation modelTreeOperation = new ModelTreeOperation();
		modelTreeOperation.selectTreeNode("数据处理", "矢量", "点聚类");
		ModelWindowOperation modelWindowOperation = new ModelWindowOperation();
		modelWindowOperation.dragToCanvasPoint(new Point(0, 60));
		//选中节点，设置参数
		modelWindowOperation.clickProcessGraph("点聚类");
		modelWindowOperation.clickRightButtonMenuItem("参数设置");
		new WorkspaceTreeOperation().selectTheDatasouceNodeMenu(dataSourceType);
		new JFileChooserOperation().chooseFile(udbPath);
		robot.delay(2000);
		DockbarOperation dockbarOperation = new DockbarOperation("参数设置");
		dockbarOperation.setComboBoxSelectedItem(new NameMatcher(CoreProperties.getString(CoreProperties.Label_Dataset)), "AggregatePoints");
		dockbarOperation.editTextField(new NameMatcher(ProcessProperties.getString("String_AggregatePoints_Distance")), "1");
		robot.delay(1000);
		dockbarOperation.setComboBoxSelectedItem(new NameMatcher(CoreProperties.getString("String_DistanceUnit_Millimeter")), "千米");
		dockbarOperation.editTextField(new NameMatcher(ProcessProperties.getString("String_AggregatePoints_MinPilePointCount")), "10");
		dockbarOperation.editTextField(new NameMatcher(CoreProperties.getString(CoreProperties.Label_Dataset) + "_textField"), "actual_aggregatePoints");
		//点击执行
		ModelOperation.runWorkFlow();
		//等待进度条 3min
		int i = modelWindowOperation.waitWorkFlowCompleted(TimeUnit.MINUTES.toMillis(3));
		Assert.assertEquals("任务执行失败！", TasksManager.WORKFLOW_STATE_COMPLETED, i);
		Boolean boolen = compareOperation.compareDataset("DataProcess_Vector", "exp_aggregatePoints", "actual_aggregatePoints");
		Assert.assertTrue("实际生成的结果与预计结果不一致！", boolen);
		new CommonOperation().deleteDatasets("DataProcess_Vector", "actual_aggregatePoints");
	}

	@Test
	public void testLinePolygonSmooth() throws Exception {
		String udbPath = DataUtil.udbPath("DataProcess/DataProcess_Vector.udb");
		String dataSourceType = "打开文件型数据源...";
		WorkspaceTreeOperation workspaceTreeOperation = new WorkspaceTreeOperation();
		workspaceTreeOperation.selectTheDatasouceNodeMenu(dataSourceType);
		new JFileChooserOperation().chooseFile(udbPath);
		robot.delay(2000);
		workspaceTreeOperation.selectDatasetNodeMenu("DataProcess_Vector", "LineSmooth|PolygonSmooth", "复制数据集...");
		DialogOperation dialogOperation = new DialogOperation("复制数据集");
		Component component = dialogOperation.searchComponent(new ClassMatcher(JTable.class));
		TableOperation tableOperation = new TableOperation((JTable) component);
		tableOperation.setCellValue(0, 3, "actual_PolygonSmooth");
		tableOperation.setCellValue(1, 3, "actual_LineSmooth");
		dialogOperation.clickComponent(new TextMatcher("复制"));
		robot.delay(5000);
		RIBBONOPERATION.clickButton("可视化建模", "新建");
		ModelTreeOperation modelTreeOperation = new ModelTreeOperation();
		modelTreeOperation.selectTreeNode("数据处理", "矢量", "线面光滑");
		ModelWindowOperation modelWindowOperation = new ModelWindowOperation();
		modelWindowOperation.dragToCanvasPoint(new Point(0, 50));
		modelTreeOperation.selectTreeNode("数据处理", "矢量", "线面光滑");
		modelWindowOperation.dragToCanvasPoint(new Point(0, -50));
		//选中节点，设置参数
		modelWindowOperation.clickProcessGraph("线面光滑");
		modelWindowOperation.clickRightButtonMenuItem("参数设置");
		DockbarOperation dockbarOperation = new DockbarOperation("参数设置");
		dockbarOperation.setComboBoxSelectedItem(new NameMatcher(CoreProperties.getString(CoreProperties.Label_Dataset)), "actual_LineSmooth");
		dockbarOperation.editTextField(new NameMatcher(ProcessProperties.getString("String_Label_Smoothness")), "5");
		modelWindowOperation.clickProcessGraph("线面光滑(1)");
		modelWindowOperation.clickRightButtonMenuItem("参数设置");
		dockbarOperation.setComboBoxSelectedItem(new NameMatcher(CoreProperties.getString(CoreProperties.Label_Dataset)), "actual_PolygonSmooth");
		dockbarOperation.editTextField(new NameMatcher(ProcessProperties.getString("String_Label_Smoothness")), "5");
		//点击执行
		ModelOperation.runWorkFlow();
		//等待进度条 3min
		int i = modelWindowOperation.waitWorkFlowCompleted(TimeUnit.MINUTES.toMillis(4));
		Assert.assertEquals("任务执行失败！", TasksManager.WORKFLOW_STATE_COMPLETED, i);
		Boolean boolen = compareOperation.compareDataset("DataProcess_Vector", "exp_LineSmooth", "actual_LineSmooth");
		Assert.assertTrue("对线数据集执行光滑处理得到的实际结果与预计结果不一致！", boolen);
		new CommonOperation().deleteDatasets("DataProcess_Vector", "actual_LineSmooth");
		Boolean boolen1 = compareOperation.compareDataset("DataProcess_Vector", "exp_PolygonSmooth", "actual_PolygonSmooth");
		Assert.assertTrue("对面数据集执行光滑处理得到的实际结果与预计结果不一致！", boolen1);
		new CommonOperation().deleteDatasets("DataProcess_Vector", "actual_PolygonSmooth");
	}


	/**
	 * 重设坐标系
	 */
	@Test
	public void testResetCoordsys() throws Exception {
		String dataPath = "DataProcess/testResetCoordsys.udb";
		String resultDsPath = DataUtil.localDataPath + dataPath;
		String title = "重设坐标系";

		//复制数据源
		Assert.assertTrue(DataUtil.copyUDB(dataPath));
		CommonOperation.openUDB(resultDsPath);
		//建模
		ModelOperation.startModeling();
		ModelTreeOperation modelTreeOperation = new ModelTreeOperation();
		ModelWindowOperation modelWindowOperation = new ModelWindowOperation();
		modelTreeOperation.selectTreeNode("坐标系设置", title);
		modelWindowOperation.dragToCanvasPoint(new Point(0, -50));

		//平面栅格设置为地理坐标系
		Operation operation = new Operation(modelWindowOperation.getContainer());
		modelWindowOperation.clickProcessGraph(title);
		operation.clickRightButtonMenuItem("参数设置");
		DockbarOperation dockbarOperation = new DockbarOperation("参数设置");
		//平面矢量设置为投影坐标系
		ModelOperation.selectSourceDataset("JingjinTerrain");
		dockbarOperation.clickComponent(new NameMatcher(ControlsProperties.getString("String_ProjectionInfoControl_ButtonResetProjectionInfo")));
		DialogOperation dialogOperation = new DialogOperation(ControlsProperties.getString("String_SetCoordsys"));
		Component jTree = dialogOperation.searchComponent(new ClassMatcher(JTree.class));
		TreeOperation.selectTreeNodes(((JTree) jTree), CoreProperties.getString("String_GeoCoordSys_Name"));
		dialogOperation.setTextComponentValue(new NameMatcher("textFieldSearch"), "1984");
		Component jTable = dialogOperation.searchComponent(new ClassMatcher(JTable.class));
		TableOperation tableOperation = new TableOperation((JTable) jTable);
		tableOperation.selectRow(1);
		dialogOperation.clickComponent(new NameMatcher("buttonApply"));
		//点击执行
		ModelOperation.runWorkFlow();
		int state = modelWindowOperation.waitWorkFlowCompleted(TimeUnit.MINUTES.toMillis(1));
		if (state != TasksManager.WORKFLOW_STATE_COMPLETED) {
			errorCollector.addError(new Throwable("Tasks not fully completed!"));
			modelWindowOperation.collectExceptionProcess(errorCollector);
		}

		//平面栅格设置为地理坐标系
		operation = new Operation(modelWindowOperation.getContainer());
		modelWindowOperation.clickProcessGraph(title);
		operation.clickRightButtonMenuItem("参数设置");
		ModelOperation.selectSourceDataset("Beijing_Road_L");
		dockbarOperation.clickComponent(new NameMatcher(ControlsProperties.getString("String_ProjectionInfoControl_ButtonResetProjectionInfo")));
		dialogOperation = new DialogOperation(ControlsProperties.getString("String_SetCoordsys"));
		jTree = dialogOperation.searchComponent(new ClassMatcher(JTree.class));
		TreeOperation.selectTreeNodes(((JTree) jTree), ControlsProperties.getString("String_PrjCoorSys"));
		dialogOperation.setTextComponentValue(new NameMatcher("textFieldSearch"), "Sphere Merca");
		jTable = dialogOperation.searchComponent(new ClassMatcher(JTable.class));
		tableOperation = new TableOperation((JTable) jTable);
		tableOperation.selectRow(0);
		dialogOperation.clickComponent(new NameMatcher("buttonApply"));
		//点击执行
		ModelOperation.runWorkFlow();
		state = modelWindowOperation.waitWorkFlowCompleted(TimeUnit.MINUTES.toMillis(1));
		if (state != TasksManager.WORKFLOW_STATE_COMPLETED) {
			errorCollector.addError(new Throwable("Tasks not fully completed!"));
			modelWindowOperation.collectExceptionProcess(errorCollector);
		}

		//结果判断
		Datasource resultDatasource = CommonOperation.getDatasource(0);
		Dataset dataset = resultDatasource.getDatasets().get("JingjinTerrain");
		if (!dataset.getPrjCoordSys().getGeoCoordSys().getType().equals(GeoCoordSysType.GCS_WGS_1984)) {
			errorCollector.addError(new Throwable("Dataset JingjinTerrain set GeoCoordSys failed:should be GCS_WGS_1984."));
		}
		dataset = resultDatasource.getDatasets().get("Beijing_Road_L");
		if (!dataset.getPrjCoordSys().getType().equals(PrjCoordSysType.PCS_SPHERE_MERCATOR)) {
			errorCollector.addError(new Throwable("Dataset Beijing_Road_L set PrjCoordSys failed:should be PCS_SPHERE_MERCATOR."));
		}
	}

	/**
	 * 投影转换
	 */
	@Test
	public void testProjectionTransform() throws Exception {
		String dataPath = "DataProcess/testProjectionTransform/";
		String udbPath = DataUtil.localDataPath + dataPath + "testProjectionTransform.udb";
		String prjFilePath = DataUtil.localDataPath + dataPath + "Clarke 1858.xml";
		String expDsPath = DataUtil.resultPath_exp + dataPath + "testProjectionTransform.udb";
		String[] datasetNames = {"BaseMap_R", "Beijing_Road_L"};
		String title = "投影转换";

		//复制数据源
		Assert.assertTrue(DataUtil.copyData(dataPath));
		CommonOperation.openUDB(udbPath);

		//建模
		ModelOperation.startModeling();
		ModelTreeOperation modelTreeOperation = new ModelTreeOperation();
		ModelWindowOperation modelWindowOperation = new ModelWindowOperation();
		modelTreeOperation.selectTreeNode("坐标系设置", title);
		modelWindowOperation.dragToCanvasPoint(new Point(0, -50));

		Operation operation = new Operation(modelWindowOperation.getContainer());
		modelWindowOperation.clickProcessGraph(title);
		operation.clickRightButtonMenuItem("参数设置");
		DockbarOperation dockbarOperation = new DockbarOperation("参数设置");
		//来自数据源
		ModelOperation.selectSourceDataset(datasetNames[0]);
		dockbarOperation.clickComponent(new TextMatcher(ControlsProperties.getString("String_Label_FromDatasource")));
		ModelOperation.setResultDatasetName("prjTransform_fromDatasource");

		ModelOperation.runWorkFlow();
		int state = modelWindowOperation.waitWorkFlowCompleted(TimeUnit.MINUTES.toMillis(1));
		if (state != TasksManager.WORKFLOW_STATE_COMPLETED) {
			errorCollector.addError(new Throwable("Tasks not fully completed!"));
			modelWindowOperation.collectExceptionProcess(errorCollector);
		}

		//来自数据集
		modelWindowOperation.clickProcessGraph(title);
		operation.clickRightButtonMenuItem("参数设置");
		dockbarOperation.clickComponent(new TextMatcher(ControlsProperties.getString("String_Label_FromDataset")));
		dockbarOperation.setComboBoxSelectedItem(new NameMatcher("datasetComboBox"), datasetNames[1]);
		ModelOperation.setResultDatasetName("prjTransform_fromDataset");

		ModelOperation.runWorkFlow();
		state = modelWindowOperation.waitWorkFlowCompleted(TimeUnit.MINUTES.toMillis(1));
		if (state != TasksManager.WORKFLOW_STATE_COMPLETED) {
			errorCollector.addError(new Throwable("Tasks not fully completed!"));
			modelWindowOperation.collectExceptionProcess(errorCollector);
		}

		//设置坐标系
		modelWindowOperation.clickProcessGraph(title);
		operation.clickRightButtonMenuItem("参数设置");
		dockbarOperation.clickComponent(new TextMatcher(ControlsProperties.getString("String_Label_ProjectionSetting")));
		dockbarOperation.clickComponent(new NameMatcher("buttonPrjSetting"));
		DialogOperation dialogOperation = new DialogOperation(ControlsProperties.getString("String_SetCoordsys"));
		Component jTree = dialogOperation.searchComponent(new ClassMatcher(JTree.class));
		TreeOperation.selectTreeNodes(((JTree) jTree), ControlsProperties.getString("String_PrjCoorSys"));
		Component jTable = dialogOperation.searchComponent(new ClassMatcher(JTable.class));
		TableOperation tableOperation = new TableOperation((JTable) jTable);
		tableOperation.selectRow(3);
		robot.delay(1000);
		new CrossRobot().doubleClick();
		robot.delay(1000);
		dialogOperation.setTextComponentValue(new NameMatcher("textFieldSearch"), "50");
		jTable = dialogOperation.searchComponent(new ClassMatcher(JTable.class));
		tableOperation = new TableOperation((JTable) jTable);
		tableOperation.selectRow(0);
		dialogOperation.clickComponent(new NameMatcher("buttonApply"));
		ModelOperation.setResultDatasetName("prjTransform_setCoordsys");

		ModelOperation.runWorkFlow();
		state = modelWindowOperation.waitWorkFlowCompleted(TimeUnit.MINUTES.toMillis(1));
		if (state != TasksManager.WORKFLOW_STATE_COMPLETED) {
			errorCollector.addError(new Throwable("Tasks not fully completed!"));
			modelWindowOperation.collectExceptionProcess(errorCollector);
		}

		//导入文件
		modelWindowOperation.clickProcessGraph(title);
		operation.clickRightButtonMenuItem("参数设置");
		dockbarOperation.clickComponent(new TextMatcher(ControlsProperties.getString("String_Label_ImportPrjCoordSysFile")));
		dockbarOperation.clickComponent(new NameMatcher("FileChooserControl_button"));
		new JFileChooserOperation().chooseFile(prjFilePath);
		ModelOperation.setResultDatasetName("prjTransform_importFile");

		ModelOperation.runWorkFlow();
		state = modelWindowOperation.waitWorkFlowCompleted(TimeUnit.MINUTES.toMillis(1));
		if (state != TasksManager.WORKFLOW_STATE_COMPLETED) {
			errorCollector.addError(new Throwable("Tasks not fully completed!"));
			modelWindowOperation.collectExceptionProcess(errorCollector);
		}
		//结果对比
		Datasource resultDatasource = CommonOperation.getDatasource(0);
		Datasource expDatasource = CommonOperation.openDatasource(expDsPath);
		compareOperation.compareDataset(expDatasource, resultDatasource);
	}

}
